using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Model.Core.Entity;
using Model.Core.Entity.Dbcontexts;
using System.Collections.ObjectModel;

namespace RobotPratolSolution.ViewModels
{
    public class MapManageViewModel : ObservableObject
    {
        private ObservableCollection<MapInfo> _mapInfoList = new();
        private MapInfo _selectedMap = new MapInfo();

        public ObservableCollection<MapInfo> MapInfoList
        {
            get => _mapInfoList;
            set { _mapInfoList = value; OnPropertyChanged(); }
            }


        public MapInfo SelectedMap
        {
            get => _selectedMap;
            set
            {
                if (_selectedMap != value)
                {
                    _selectedMap = value; OnPropertyChanged();
                }
            }

        }

        private string _MapName;

        public string MapName
        {
            get { return _MapName; }
            set
            {
                if (_MapName != value)
                {
                    _MapName = value; OnPropertyChanged();
                }
            
            }
        }



        public RelayCommand CreateCommand { get; }
        public RelayCommand DeleteCommand { get; }
        public RelayCommand ModifyCommand { get; }
        public RelayCommand RefreshCommand { get; }
        DbcontextBase DbcontextBase { get; set; }

        public MapManageViewModel()
        {
            DbcontextBase = App.ServiceProvider.GetService(typeof(DbcontextBase)) as DbcontextBase;

            CreateCommand = new RelayCommand(OnCreate);
            DeleteCommand = new RelayCommand(OnDelete);
            ModifyCommand = new RelayCommand(OnModify);
            RefreshCommand = new RelayCommand(OnRefresh);
            LoadMapData();
        }

        private async void LoadMapData()
        {
            var maps = DbcontextBase.MapInfo.ToList();
            MapInfoList = new ObservableCollection<MapInfo>(maps);
        }

        private async void OnCreate()
        {
            var newMap = new MapInfo { Name = MapName, Id = Guid.NewGuid() };
            DbcontextBase.MapInfo.Add(newMap);
            await DbcontextBase.SaveChangesAsync();
            MapInfoList.Add(newMap);
        }

        private async void OnDelete()
        {
            if (SelectedMap != null)
            {
                DbcontextBase.MapInfo.Remove(SelectedMap);
                await DbcontextBase.SaveChangesAsync();
                MapInfoList.Remove(SelectedMap);
                if (MapInfoList.Count == 0)
                {
                    SelectedMap = new MapInfo();
                }
            }
        }

        private async void OnModify()
        {
            if (SelectedMap != null)
            {
                DbcontextBase.MapInfo.Update(SelectedMap);
                await DbcontextBase.SaveChangesAsync();
                OnPropertyChanged("MapInfoList");
            }
        }

        private void OnRefresh()
        {
            LoadMapData();
        }
    }
}
